package cn.hxzy.office.controller;

import cn.hxzy.office.entity.User;
import cn.hxzy.office.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author little black
 * 登录，登出
 */
@Controller
//@SessionAttributes("loginUser")
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
//    用户登录
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
//        modelAndView.addObject("info", "账户或密码有误");
        return modelAndView;
    }

    @PostMapping("/userLogin")
    public String userLogin(User user, HttpSession session) {
        User user1 = userService.login(user);
        if (user1 != null) {
            if (user1.getRoleId() == 0) {
                session.setAttribute("loginUser", user);
                return "redirect:/";
            }
            if (user1.getRoleId() == 1) {
                session.setAttribute("loginUser", user);
                return "redirect:/userIndex";
            }

        }
        session.setAttribute("info","账户或密码有误");
        return "redirect:/login";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }

}
